#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <glob.h>
#include <syslog.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <mytbf.h>
#include <proto.h>

#include "medialib.h"
#include "mainconf.h"
#include "conf.h"

struct channel_status_st {
	struct chn_entry_st ent;
	glob_t glob;
	int curr;
	off_t pos;
	int fd;
	mytbf_ctx_t *tbf;
};

static struct channel_status_st channel_status[CHNMAX];
static int channel_num=0;

static chnid_t curr_id;

static int medialib_build_channels(void)
{
	int i;
	glob_t glob_mdir, glob_cdir;
	char path[PATHMAX];

	if (snprintf(path, PATHMAX, "%s/*", configure.media_dir)>=PATHMAX) {
	    syslog(LOG_ERR, "%s/%s/%d: media_dir is too long.", __FILE__, __FUNCTION__, __LINE__);
		exit(1);
	}

	if (glob(path, 0, NULL, &glob_mdir)) {
	    syslog(LOG_ERR, "%s/%s/%d: glob() failed.", __FILE__, __FUNCTION__, __LINE__);
		exit(1);
	}

	if (glob_mdir.gl_pathc<=0) {
		syslog(LOG_ERR, "%s/%s/%d: No channel dir found!");
		exit(1);
	}

	curr_id = CHNID_MIN;
	channel_num = 0;
	for (i=0;i<glob_mdir.gl_pathc;++i) {
		snprintf(path, PATHMAX, "%s/%s", glob_mdir.gl_pathv[i], DEFAULT_DESCFNAME);
		if (access(path, R_OK)) {
			syslog(LOG_DEBUG, "%s/%s/%d: %s is not a channel dir.", __FILE__, __FUNCTION__, __LINE__, path);
			continue;
		}

		if (snprintf(path, PATHMAX, "%s/*.mp3", configure.media_dir)>=PATHMAX) {
			syslog(LOG_ERR, "%s/%s/%d: media_dir is too long.", __FILE__, __FUNCTION__, __LINE__);
			exit(1);
		}
		if (glob(path, 0, NULL, &channel_status[channel_num].glob)) {
			syslog(LOG_ERR, "%s/%s/%d: glob() failed.", __FILE__, __FUNCTION__, __LINE__);
			exit(1);
		}
		if (channel_status[channel_num].glob.gl_pathc<=0) {
			syslog(LOG_DEBUG, "%s/%s/%d: channel %s dir is empty.", __FILE__, __FUNCTION__, __LINE__, path);
			continue;
		}
		/* Init this channel */
		channel_status[channel_num].ent.chnid = curr_id;
		strcpy(channel_status[channel_num].ent.desc, "TEST");
		channel_status[channel_num].curr = 0;
		channel_status[channel_num].pos = 0;
		channel_status[channel_num].fd = open(channel_status[channel_num].glob.gl_pathv[channel_status[channel_num].curr], O_RDONLY);
		if (channel_status[channel_num].fd <0) {
			syslog(LOG_WARNING, "%s/%s/%d: open(): %s", __FILE__, __FUNCTION__, __LINE__, strerror(errno));
			continue;
		}
		channel_status[channel_num].tbf = mytbf_ctx_init(8192, 10000);
		if (channel_status[channel_num].tbf == NULL) {
			syslog(LOG_WARNING, "%s/%s/%d: mytbf_ctx_init(): %s", __FILE__, __FUNCTION__, __LINE__, strerror(errno));
			close(channel_status[channel_num].fd);
			continue;
		}
		++channel_num;
	}

	globfree(&glob_mdir);
	syslog(LOG_ERR, "%s/%s: malloc() failed.", __FILE__, __FUNCTION__);
}

int medialib_get_chnlist(struct chn_entry_st **res, int *chn_nr)
{
	int i;
	medialib_build_channels();

	*res = calloc(channel_num, sizeof(struct chn_entry_st));
	if (*res==NULL) {
		syslog(LOG_ERR, "%s/%s: malloc() failed.", __FILE__, __FUNCTION__);
		return -1;
	}
	for (i=0;i<channel_num;++i) {
		(*res[i]).chnid = channel_status[i].ent.chnid;
		strncpy((*res[i]).desc, channel_status[i].ent.desc, DESCMAX);
	}
	*chn_nr = channel_num;

	return 0;
}

int medialib_free_chnlist(struct chn_entry_st *res)
{
	free(res);
	return 0;
}

ssize_t medialib_read_chn(chnid_t id, void *ptr, size_t size)
{
}


